package com.base.vistter.iframe.inverter.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.base.vistter.iframe.bean.Result;
import com.base.vistter.iframe.entity.InverterVersion;
import com.base.vistter.iframe.exception.IframeException;
import com.base.vistter.iframe.service.InverterVersionService;
import com.base.vistter.iframe.utils.IoUtils;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/version")
public class InverterVersionController {

    public static final String FILE_PATH = "/opt/inverter/app/";

    @Resource(name = "inverterVersionService")
    private InverterVersionService inverterVersionService;

    @GetMapping
    public Result<Page<InverterVersion>> get(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(value = "pageNum", defaultValue = "10") Integer pageNum) throws IframeException {
        Page<InverterVersion> page = Page.of(pageNo, pageNum);
        LambdaQueryWrapper<InverterVersion> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.orderByDesc(InverterVersion::getVersion);
        return Result.ok(inverterVersionService.page(page, lambdaQueryWrapper));
    }

    @GetMapping("/{id}")
    public Result<InverterVersion> get(@PathVariable("id") Integer id) throws IframeException {
        return Result.ok(inverterVersionService.getById(id));
    }

    @PostMapping(produces = "application/json;charset=utf-8")
    public Result<Boolean> save(@RequestBody InverterVersion inverterVersion) throws IframeException {
        return Result.ok(inverterVersionService.save(inverterVersion));
    }

    @PutMapping(produces = "application/json;charset=utf-8")
    public Result<Boolean> update(@RequestBody InverterVersion inverterVersion) throws IframeException {
        return Result.ok(inverterVersionService.update(inverterVersion));
    }

    @DeleteMapping("/{id}")
    public Result<Integer> delete(@PathVariable("id") Integer id) throws IframeException {
        return Result.ok(inverterVersionService.delete(id));
    }

    @GetMapping("/latest")
    public Result<InverterVersion> latest() throws IframeException{
        LambdaQueryWrapper<InverterVersion> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.orderByDesc(InverterVersion::getVersion);
        return Result.ok(inverterVersionService.selectOne(lambdaQueryWrapper));
    }

    @GetMapping("/download")
    public void download(String fileName, HttpServletResponse response) throws IframeException{
        LambdaQueryWrapper<InverterVersion> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.orderByDesc(InverterVersion::getVersion);
        IoUtils.fileToStream(FILE_PATH + fileName , response);
    }

}
